package com.ldc.wandroid.ui.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.PersoanlRankAdapter;
import com.ldc.wandroid.databinding.ActivityPersonalRankBinding;
import com.ldc.wandroid.model.PersonalRankModel;
import com.ldc.wandroid.viewmodel.PersonalRankViewModel;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;

import java.util.List;

public class PersonalRankActivity extends BaseMvvmActivity<ActivityPersonalRankBinding, PersonalRankViewModel> {


    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, PersonalRankActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    //
    private final PersoanlRankAdapter persoanlRankAdapter = new PersoanlRankAdapter();
    //
    private int currIndex = 1;
    //
    private static final int REFRESH_DT_CODE = 0x000;


    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewModel.getCoinRank().removeObserver(this::getCoinRankResp);
    }

    @Override
    protected ActivityPersonalRankBinding initViewBinding() {
        return ActivityPersonalRankBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        viewBinding.topBar.tvTitle.setText(String.format("%s", "积分排行榜"));
        viewBinding.topBar.lineMore.setVisibility(View.GONE);
        viewBinding.topBar.lineBack.setOnClickListener(v -> finish());
        //
        initAdapter();
        //
        viewBinding.rv.refreshView.setOnLoadMoreListener(onLoadMoreListener);
        viewBinding.rv.refreshView.setEnableRefresh(false);
        // 加载数据
        viewModel.getCoinRank().observeForever(this::getCoinRankResp);
        viewModel.getCoinRankReq(currIndex);
    }


    public void getCoinRankResp(List<PersonalRankModel.DatasBean> dt) {
        viewBinding.stateView.showContentView();
        viewBinding.rv.refreshView.finishLoadMore();
        if (currIndex == 1) {
            persoanlRankAdapter.setNewData(dt);
        } else {
            persoanlRankAdapter.addNewData(dt);
        }
    }


    // 初始化适配器
    private void initAdapter() {
        viewBinding.rv.recyclerView.setHasFixedSize(true);
        viewBinding.rv.recyclerView.setItemViewCacheSize(10);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        viewBinding.rv.recyclerView.setLayoutManager(layoutManager);
        viewBinding.rv.recyclerView.setAdapter(persoanlRankAdapter);

    }

    //下拉刷新事件
    private final OnLoadMoreListener onLoadMoreListener = refreshLayout -> {
        currIndex += 1;
        viewModel.getCoinRankReq(currIndex);
    };

    @Override
    protected PersonalRankViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, PersonalRankViewModel.class);
    }
}
